import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import setaxios from './setaxios'

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

setaxios()
Vue.config.productionTip = false // 是否提示错误 DEBUG

Vue.prototype.$axios=axios

//测试环境
axios.defaults.baseURL = 'http://www.hunpuo.com'
Vue.prototype.api_url ='http://www.hunpuo.com'//全局变量

//正式环境
// axios.defaults.baseURL = 'http://api.hunpuo.com'
// Vue.prototype.api_url ='http://api.hunpuo.com'//全局变量


//全局函数
//获取用户信息
Vue.prototype.getUserInfo = function (){//changeData是函数名
  //window.localStorage.setItem('user_token',JSON.stringify(user_info));
  let userInfo = window.localStorage.getItem('userInfo');
  if(userInfo){
  	 return JSON.parse(userInfo);
  }
  return userInfo;
}
//获取token
Vue.prototype.getUserToken = function (){//changeData是函数名
  let userInfo = window.localStorage.getItem('userInfo');
  let user_token = null
  if(userInfo){
     user_token = JSON.parse(userInfo).user_token;

  }
  return user_token;
}
//时间格式化
  Vue.prototype.getFormat = function (msec){
      let ss = parseInt(msec / 1000);
      let ms = parseInt(msec % 1000);
      let mm = 0;
      let hh = 0;
      if (ss > 60) {
        mm = parseInt(ss / 60);
        ss = parseInt(ss % 60);
        if (mm > 60) {
          hh = parseInt(mm / 60);
          mm = parseInt(mm % 60);
        }
      }
      ss = ss > 9 ? ss : `0${ss}`;
      mm = mm > 9 ? mm : `0${mm}`;
      hh = hh > 9 ? hh : `0${hh}`;
      return { ms, ss, mm, hh };
    }
  
  //时间格式化
  Vue.prototype.toMoney =  function (num){
    if(num){
        if(isNaN(num)) {
          
            return;
        }
        num = typeof num == "string"?parseFloat(num):num//判断是否是字符串如果是字符串转成数字
        num = num.toFixed(2);//保留两位
        num = parseFloat(num);//转成数字
        num = num.toLocaleString();//转成金额显示模式
        //判断是否有小数
        if(num.indexOf(".")==-1){
            num = "￥"+num+".00";
        }else{
            num = num.split(".")[1].length<2?"￥"+num+"0":"￥"+num;
        }
        return num;//返回的是字符串23,245.12保留2位小数
    }else{
        return num = null;
    }
  } 

  //路由守卫
  router.beforeEach((to,from,next)=>{
    //如果未匹配到路由
    if (to.matched.length === 0) { 
        from.name ? next({ name: from.name }) : next('/error'); 
    }

    //无论是刷新还是跳转路由，第一个进入的就是这个路由前置钩子函数
    if(to.meta.requireAuth){
      let user_token = Vue.prototype.getUserToken()
      if(user_token){
        next()
      }else{
        next({
          path:'/register/login',
          query:{redirect:to.fullPath}
        })
      }
    }else{
      next()
    }
  })


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
